package ibmetp.mis.controller.role;

import ibmetp.mis.common.Constants;
import ibmetp.mis.service.PageService;
import ibmetp.mis.service.RoleService;
import ibmetp.mis.serviceImpl.RoleServiceImpl;
import ibmetp.mis.vo.Page;
import ibmetp.mis.vo.ResNode;
import ibmetp.mis.vo.Resource;
import ibmetp.mis.vo.Role;
import ibmetp.mis.vo.User;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class RoleTreeServlet
 */
public class RoleTreeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoleTreeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("RoleTreeServlet");
		
		String id=request.getParameter("id")!=null?request.getParameter("id"):"0";
		Long pid=Long.valueOf(id);
		if(pid>0) return;
		PageService ps=new RoleServiceImpl();
		Page p=ps.getPage(1, 999, new Role());
		
		JSONArray o=new JSONArray();
		
		//得到pid的子节点
		List<Role> list=p.getList();
		for(int i=0;i<list.size();i++)
		{    Role r=list.get(i);
		     ResNode rn=new ResNode();
	         rn.setId(r.getRoleId());
		     rn.setText(r.getRoleName());
		     rn.setState("open");
		     o.add(rn);
		}	
		
		//返回JSON数据
		response.setCharacterEncoding(Constants.ENCODING_UTF8);
		response.getWriter().write(o.toString());
		System.out.println(o.toString());
	}

}
